﻿<# 
.SYNOPSIS 
    Removes a PeopleSoft Application Server domain.
.DESCRIPTION 
    The command Remove-PsftDomain will completely remove a PeopleSoft Application Server domain.
    This command requires that the application server be stopped.
.NOTES 
    File Name  : Register-PsftConfiguration.ps1 
    Author     : Scott Kearney - scott@kearney.tk 
    Requires   : PowerShell V3 
.EXAMPLE 
    Register-PsftConfiguration -PS_HOME C:\psoft\PS_HOME -PS_CFG_HOME C:\psoft\PS_CFG_HOME -ServerType AppServer -Domain DOMAIN1
    Registers the configuration for DOMAIN1.
    Will automatically stop the domain if it is running.
.PARAMETER PS_HOME
    The full path to the PS_HOME directory for this domain.
.PARAMETER PS_CFG_HOME
    The full path to the PS_CFG_HOME directory for this domain.
.PARAMETER ServerType
    The type of server on which to perform the action.
.PARAMETER Domain
    The name of the domain on which to perform the action.
#> 
function Remove-PsftDomain
{
    [CmdletBinding(SupportsShouldProcess=$True,ConfirmImpact='High')]
    Param
    (
        # The full path to the PS_HOME directory for this domain.
        # See http://docs.oracle.com/cd/E41633_01/pt853pbh1/eng/pt/tsvt/concept_UnderstandingPS_HOMEandPS_CFG_HOME-eb7ece.html
        [Parameter(Mandatory=$true,
                   ValueFromPipelineByPropertyName=$true,
                   HelpMessage = "The full path to the PS_HOME directory for this domain.",
                   Position=0)]
        [ValidateScript({Validate_PS_HOME $_})]
        [String]
        $PS_HOME,

        # The full path to the PS_CFG_HOME directory for this domain.
        # See http://docs.oracle.com/cd/E41633_01/pt853pbh1/eng/pt/tsvt/concept_UnderstandingPS_HOMEandPS_CFG_HOME-eb7ece.html
        [Parameter(Mandatory=$true,
                   ValueFromPipelineByPropertyName=$true,
                   HelpMessage = "The full path to the PS_CFG_HOME directory for this domain.",
                   Position=1)]
        [ValidateScript({Validate_PS_CFG_HOME $_})]
        [String]
        $PS_CFG_HOME,

        # The type of server on which to perform the action.
        [Parameter(Mandatory=$true,
                   ValueFromPipelineByPropertyName=$true,
                   HelpMessage = "The type of server on which to perform the action.",
                   Position=2)]
        [ValidateSet("AppServer","PrcsScheduler","SearchServer","WebServer")]
        $ServerType,

        # The name of the domain on which to perform the action.
        # Should be a directory under the PS_CFG_HOME
        [Parameter(Mandatory=$true,
                   ValueFromPipelineByPropertyName=$true,
                   HelpMessage = "The name of the domain on which to perform the action.",
                   Position=3)]
        [ValidateScript({Validate_Domain $PS_CFG_HOME $ServerType $_})]
        [String]
        $Domain
    )

    Begin
    {
    }
    Process
    {
        # Set up variables
        $PSAdmin = @{}
        $return = ""

        # The command name changes based on the $ServerType
        switch ($ServerType)
        {
            'AppServer'     {$command = "delete"}
            'PrcsScheduler' {$command = "delete"}
            'SearchServer'  {$command = "delete"}
            'WebServer'     {$command = "remove"}
        }

        # Ensure that the user would like us to perform this potentially destructive action.
        if (($pscmdlet.ShouldProcess("$Domain", "Remove-PsftDomain")))
        {
            Write-Verbose "Confirmed to remove the domain $Domain."

            # If the domain is running, the domain cannot be removed.
            if(Get-PsftStatus -PS_HOME $PS_HOME -PS_CFG_HOME $PS_CFG_HOME -ServerType $ServerType -Domain $Domain -Target Server)
            {
                Write-Verbose "The application server is running. Confirm that the application server will be shut down."

                $null = Stop-PsftDomain -PS_HOME $PS_HOME -PS_CFG_HOME $PS_CFG_HOME -ServerType $ServerType -Domain $Domain
            }

            Write-Verbose "Removing domain..."

            # Invokes PSAdmin.exe to remove the specified domain.
            $PSAdmin = Invoke-PSAdmin -PS_HOME $PS_HOME -PS_CFG_HOME $PS_CFG_HOME -ServerType $ServerType -Domain $Domain -Command $command

            switch($PSAdmin.Result)
            {
                {$_ -match "Unable to delete domain files"}                 { throw "Unable to remove the domain $Domain. Ensure that the domain is stopped and try again." }
                {$_ -match "Please shutdown the domain before deleting it"} { throw "The Application Server must be shut down to remove the domain $Domain." }
            }
            
            $return =  @{
                            PS_HOME = $PS_HOME;
                            PS_CFG_HOME = $PS_CFG_HOME;
                            Domain = $Domain;
                            Action = "Remove-PsftDomain";
                            Status = "Success";
                        }
        
        }

        return $return
    }
    End
    {
    }
}